//app.js// fljp928bkd  wx38e3ce9a3b64a91c
var rqcfg = require('./utils/constant.js');
var common = require('./utils/common.js');
const {deepClone,makeCallphone,splitArrData,formatPlayTime,getFlatternDistance,getRad,sortArr,checkUpdate} = require('./utils/util');
let utils = {deepClone,makeCallphone,splitArrData,formatPlayTime,getFlatternDistance,getRad,sortArr};
// var aldstat = require('./utils/san.js');
// import { request } from './utils/request.js'
var navigateTo = wx.navigateTo;
Object.defineProperty(wx, 'navigateTo', {
  configurable: true,
  enumerable: true,
  writable: true,
})

wx.navigateTo = function(data){
  setTimeout(function(){
    navigateTo(data)
  },100)
}

let log = console.log;
let warn = console.warn;
// 重写console.log production环境将不输出日志
console.log=function(){
  if(rqcfg.env=="development"||rqcfg.isDebug){
    log(...arguments);
  }
}
console.warn=function(){
  if(rqcfg.env=="development"||rqcfg.isDebug){
    warn(...arguments);
  }
}

App({
  data:{
    
  },
  globalData: {
    userInfo: null,
    plumSession: null,
    requestUrl: rqcfg.rqcfg.rqurl,
    domin: rqcfg.rqcfg.domin,
    shareInfo: '',
    sessionFailNum:0,
    isLogining: false,  //正在登录中
    requestQueue: [],
    againRequestContact:false,
    isRequestMark: false,
    requestCartNum:-1,
    requestTmplIds: {}, //订阅ids全局变量
    saveTmplNum: 0, //全局触发保存订阅id的次数
    saveTmplArr: {}, //要保存的订阅ids,
    temporaryParameter:{}, // 临时携带参数
    statusBarHeight:0,
    titleBar:44,
    tabBarHeight:0
  },
  onLaunch: function (e) {
    var that = this;
    checkUpdate().catch(err=>{  //版本更新检测
      console.error(err);
    })
    console.log("场景值"+e.scene)
    if(e&&e.scene){
      var scene = e.scene;
      if (scene == '1007' || scene == '1008' || scene == '1047' || scene == '1048' ||scene == '1049' ||scene == '1043' || scene == '1058' || scene == '1067' || scene == '1074' || scene == '1082' || scene =='1091'){
        that.globalData.enterFrom = 'share';
      }
    }
    if (rqcfg.rqcfg.suid) {
      that.globalData.requestUrl = rqcfg.rqcfg.rqurl + 'suid=' + rqcfg.rqcfg.suid;
      rqcfg.rqcfg.rqurl = rqcfg.rqcfg.rqurl + 'suid=' + rqcfg.rqcfg.suid + '&appid=' + rqcfg.rqcfg.appid;
      that.globalData.suid = rqcfg.rqcfg.suid;
      that.globalData.appid = rqcfg.rqcfg.appid;
      that.globalData.version = rqcfg.rqcfg.version;
      // that.globalData.develop = true;
      // 微信授权获取信息
      // that.wechatSq();
    } else {
      let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
      if (extConfig) {
        console.log(extConfig);
        var suid = extConfig.suid;
        var version = extConfig.version;
        var base = extConfig.base;
        var appid = extConfig.appid ? extConfig.appid : '';
        that.globalData.menuTitle = extConfig.menu;
        that.globalData.pageviewlink = extConfig.pageviewlink;
        that.globalData.cartIndex = extConfig.cartIndex;
        rqcfg.rqcfg.rqurl = rqcfg.rqcfg.rqurl + 'suid=' + suid + '&appid=' + appid;
        that.globalData.requestUrl = rqcfg.rqcfg.rqurl;
        that.globalData.suid = suid;
        that.globalData.appid = appid;
        that.globalData.version = version;
        that.globalData.base = base;
        that.globalData.navBgcolor = extConfig.navigationBarBackgroundColor;
        that.globalData.navTextStyle = extConfig.navigationBarTextStyle;
        that.globalData.indexUrl = extConfig.indexUrl;
        // 微信授权获取信息
        // that.wechatSq();
      }
    }
    wx.onAppHide(function () { //监听小程序切入后台
      that.toSaveSubId(); //当订阅id保存接口出发不大于10次且退出小程序时，保存订阅id
    })

    const tabPages = ['index','mycartTab','myorderTab','wodeNew'];
    wx.onAppRoute((route)=>{
      let curPath = route.path.split('/').pop();
      if(tabPages.some(page=>page==curPath)){   //是tab页，则设置订单红点
        that.setOrderMark();
      }
    })
    // 获取状态栏高度，以及标题栏高度
    wx.getSystemInfo({
      success (res) {
        that.globalData.statusBarHeight=res.statusBarHeight;
        if(res.platform=="ios"){
          that.globalData.titleBar=44;
        }else if(res.platform=="android"){
           that.globalData.titleBar=48;
        }else{
           that.globalData.titleBar=44;
        }
      }
    })
    //获取底部tabBar的高度
    let systemInfo = wx.getSystemInfoSync()
    // window的高度
    let ktxWindowHeight = systemInfo.windowHeight 
    // 屏幕的高度
    let ktxScreentHeight = systemInfo.screenHeight
    // 底部tabBar的高度
    let tabBarHeight = ktxScreentHeight - ktxWindowHeight
    console.log('tabbar',systemInfo)
    that.globalData.tabBarHeight = tabBarHeight;

    wx.setStorageSync('a',-1)
  },
  onShow:function(e){
    var that = this;
    console.log('首页进入值打印==',e);
    console.log("场景值" + e.scene);
    if (e && e.query.scene){  //接收成为分销商的参数
      that.globalData.scene = decodeURIComponent(e.query.scene);
    }
    if (!that.globalData.plumSession){
      console.log("第二次发起登录请求====", e);
      //e = { form_source: 'wx19e0b0c11c111add', mid:'6793718'};
      that.wechatSq(that,e);
    }else{
      if (that.globalData.nologin != 1) {
        that.isLogin(e);
      };
      if (that.globalData.scene){
        that.requestdistributionInfo(that.globalData.scene);
      }
    } 

  },
  //监听错误
  onError: function (msg) {
    var that = this;
    console.log('错误监听');
    var errorInfo = JSON.stringify(msg);
    console.log(errorInfo);
    that.postError(errorInfo); //错误发送
  },
  ...common,  //通用方法提取到common里去了，包含一些耦合度高的函数
  ...utils, //耦合度低的工具函数提取到utils
})